// subpackages/pages/index/index.js
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    provinceList: [],
    cityList: [],
    areaList: [],
    streetList: [],
    province: null,
    provinceCode: null,
    city: null,
    cityCode: null,
    area: null,
    areaCode: null,
    street: null,
    streetCode: null,
    region: '',
    regionCode: '',
    pickerType: 'person',
    tab: {} //tab组件里的值
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //加載弹窗
    this.protocolChoose = this.selectComponent("#protocolChoose")
    //加載loading框
    this.myLoading = this.selectComponent("#loadingComponent")
    // 加载区域选框
    this.areaComponent = this.selectComponent("#area-component")
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      pickerType: 'person',
      changeId: 'id',
      loadingTip: '加载中...'
    })
    this.handleSetTabbar()
  },

  handleArea() {
    // 第一级有数据
    const provinceOptions = [
      {
        id: 520000,
        name: '贵州省'
      },
      {
        id: 440000,
        name: '广东省'
      },
      {
        id: 450000,
        name: '广西壮族自治区'
      }
    ]
    this.setData({
      provinceList: provinceOptions
    })
    this.areaComponent.showPicker()
  },

  /**
   * 选择区域第一级返回
   */
  showAreaTwo(e) {
    var name = e.detail.name;
    var code = e.detail.code;
    this.setData({
      province: name,
      provinceCode: code
    })
    // this.regionByCode(code, 2) // 获取下一级内容
    // 第二级有数据
    const cityOptions = [
      {
        id: 520000,
        name: '广州市'
      },
      {
        id: 440000,
        name: '深圳市'
      },
      {
        id: 450000,
        name: '佛山市'
      }
    ]
    this.setData({
      cityList: cityOptions
    })
    this.areaComponent.showPicker()
  },

  /**
   * 选择第二级返回
   */
  showAreaThree(e) {
    var name = e.detail.name;
    var code = e.detail.code;
    this.setData({
      city: name,
      cityCode: code,
    })
    // this.regionByCode(code, 3)
    // 第三级有数据
    const areaOptions = [
      {
        id: 520000,
        name: '天河区'
      },
      {
        id: 440000,
        name: '白云区'
      },
      {
        id: 450000,
        name: '越秀区'
      },
      {
        id: 460000,
        name: '番禺区'
      }
    ]
    this.setData({
      areaList: areaOptions
    })
    this.areaComponent.showPicker()
  },
  /**
   * 选择第3级返回
   */
  showAreaFour(e) {
    var name = e.detail.name;
    var code = e.detail.code;
    this.setData({
      area: name,
      areaCode: code,
    })
    this.regionByCode(code, 4)
  },
  /**
   * 选择第三级返回
   */
  comfirmAreaThree(e) {
    var that = this;
    var name = e.detail.name;
    var code = e.detail.code;
    this.setData({
      area: name,
      areaCode: code,
      street: '',
      streetCode: '',
    })
    console.log('第三层级触发了', e)
  },

  /**
   * 选择第4级返回
   */
  comfirmAreaFour(e) {
    console.log('第四层级有被触发了吗？', e)
    var that = this;
    var name = e.detail.name;
    var code = e.detail.code;
    this.setData({
      street: name,
      streetCode: code
    })
    var region = this.data.province + this.data.city + this.data.area + name
    var areaInfo = this.data.personInfo
    areaInfo.province = that.data.province,
      areaInfo.city = that.data.city,
      areaInfo.area = that.data.area,
      areaInfo.street = name,
      areaInfo.provinceCode = that.data.provinceCode,
      areaInfo.cityCode = that.data.cityCode,
      areaInfo.areaCode = that.data.areaCode,
      areaInfo.streetCode = code,
      this.setData({
        region: region,
        regionCode: code,
        personInfo: areaInfo,
        provinceCityArea: region,
      })
  },
  /**
   * 根据code 获取区域
   */
  regionByCode(id, type) {
    //联网获取区域列表
    var that = this;
    that.dialog.showDialog();
    getApp().globalData.api.regionByCode(id, function (message, res) {
      that.dialog.hideDialog();
      if (res) {
        var list = res.data;
        switch (type) {
          case 1:
            that.setData({
              provinceList: list
            })
            that.areaComponent.showPicker()
            break;
          case 2:
            that.setData({
              cityList: list
            })
            break;
          case 3:
            that.setData({
              areaList: list
            })
            break;
          case 4:
            that.setData({
              streetList: list
            })
            break;
          default:
            break;
        }
      } else {
        that.toastComponent.showToastComponent(message)
      }
    })
  },

  handleLoading() {
    this.myLoading.showLoading()
  },

  preRegister() {
    this.protocolChoose.showPicker()
  },

  comfirmProblem() {},
  comfirm() {
    wx.showToast({
      title: 'title',
    })
  },

  /**
   * 打开隐私政策
   */
  toPrivacy() {
    // 前往隐私协议页面
    wx.navigateTo({
      url: '/package/pages/privacyProtocol/privacyProtocol',
    })
    // var url = this.data.imgUrl + "jyfw/qzzpresource/images/private.docx"
    // // 下载文件
    // var downloadFile = ''
    // // 把文件下载到一个临时文件中
    // const downloadTask = wx.downloadFile({
    //   url: url,
    //   success: function (res) {
    //     // 下面是临时文件的路径
    //     const tempFilePath = res.tempFilePath;
    //     wx.saveFile({
    //       tempFilePath,
    //       success: function (res) {
    //         const savedFilePath = res.savedFilePath;
    //         // 打开文件
    //         wx.openDocument({
    //           filePath: savedFilePath,
    //           showMenu:true,
    //           success: function (res) {
    //           },
    //         });
    //       },
    //       fail: function (err) {
    //       }
    //     })
    //   },
    //   fail: function (err) {
    //   }
    // })
  },

  /**
   * 打开个人用户协议
   */
  toPersonPrivacy() {
    // // 前往用户协议页面
    wx.navigateTo({
      url: '/package/pages/userProtocol/userProtocol',
    })
  },

  handleSetTabbar(userType) {
    if (userType == 1 || userType == 2) {
      this.setData({
        ['tab.tab2name']: '人才',
        ['tab.isShow']: true,
      })
    } else if (userType == 4) {
      /*  this.getTabBar().setData({
         isShow: false
       }) */
      this.setData({
        ['tab.isShow']: false,
      })
    } else {
      this.setData({
        ['tab.tab2name']: '消息',
        ['tab.isShow']: true,
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})